package com.coding.basic.queue;

/**
 * 用数组实现循环队列
 *
 * @author liuxin
 */
public class CircleQueue<E> {

    private final static int DEFAULT_SIZE = 10;

    // 用数组来保存循环队列的元素
    private Object[] elementData = new Object[DEFAULT_SIZE];

    // 队头
    private int front = 0;

    // 队尾
    private int rear = 0;

    // 队列大小
    private int size = 0;

    public boolean isEmpty() {
        return false;
    }

    public boolean isFull() {
        return size == elementData.length;
    }

    public int size() {
        return size;
    }

    public void enQueue(E data) {
        if (isFull()) {
            throw new RuntimeException("The queue is full!");
        }

        rear = (rear + 1) % elementData.length;
        elementData[rear++] = data;
        size++;
    }

    public E deQueue() {
        if (isEmpty()) {
            throw new RuntimeException("The queue is empty!");
        }

        E data = (E) elementData[front];
        elementData[front] = null;
        front = (front + 1) % elementData.length;
        size--;
        return data;
    }
}
